/******************************************************************************/
/*                                                                            */
/*  Copyright (c) 2009: ngi-central.org                                       */
/*  All rights reserved.                                                      */
/*                                                                            */
/******************************************************************************/


/// \file
/// This file implements the ray geometric primitive.

#ifndef __sgl_ray_h__
#define __sgl_ray_h__

#include <sgl/global_settings.h>
#include <sgl/conditions.h>
#include <sgl/geometric_primitive.h>
#include <sgl/point.h>
#include <sgl/direction.h>
#include <sgl/vector.h>
#include <boost/static_assert.hpp>
#include <boost/operators.hpp>
#include <boost/concept_check.hpp>

namespace sgl {


/******************************************************************************/
/*                                                                            */
/* ray                                                                        */
/*                                                                            */
/******************************************************************************/

template <
	/// The dimensionality. 
	//!
	//! This defaults to two dimensions.
	size_t D=2,
	/// The element type. 
	//! 
	//! This type defaults to SGL_FPTYPE (which in turn defaults to double), 
	//! but any other numeric type is also suitable.
	typename T=SGL_FPTYPE>
/// A ray in euclidean space.
//!
//! A ray originates at some point and is then cast along some direction. Thus, 
//! it can be implemented with a combination of a point and a direction.
class ray
	/// A ray is a geometric primitive.
	: public geometric_primitive<D,T>
	, boost::equality_comparable<sgl::ray<D,T>, sgl::ray<D,T>
	, boost::additive<sgl::ray<D,T>, sgl::vector<D,T>
	> >
{
	// A ray must have two or more dimensions.
	BOOST_STATIC_ASSERT(D >= 2);

public:
	/// Default constructor.
	//!
	//! Constructs a zero ray.
	ray()
		: origin_()
		, direction_()
	{ }

	/// Construct a ray from a point and a direction.
	ray(
		/// The origin of the ray.
		sgl::point<D,T> origin,
		/// The direction of the ray.
		sgl::direction<D,T> direction)
		: origin_(origin)
		, direction_(direction)
	{ }

	/// Copy constructor.
	ray(
		/// The right hand side.
		sgl::ray<D,T> const& rhs)
		: origin_(rhs.origin_)
		, direction_(rhs.direction_)
	{ }

	/// Copy assignment operator.
	sgl::ray<D,T> & operator = (
		/// The right hand side.
		sgl::ray<D,T> rhs)
	{
		swap(rhs);
		return *this;
	}

	/// Exchanges the elements of two ray objects.
	void swap(
		/// The other ray.
		sgl::ray<D,T> & rhs)
	{
		SGL_PRECONDITION(D == rhs.dimensions(),
			"dimension mismatch.");

		std::swap(origin_, rhs.origin_);
		std::swap(direction_, rhs.direction_);
	}

	/// Find the type of the geometric primitive.
	//!
	//! /return The type of the primitive.
	geometric_type type() const
	{
		return sgl::geometric_primitive<D,T>::ray;
	} 

	/// Access the origin of the ray.
	//!
	//! /return Returns a reference to the origin of the ray.
	sgl::point<D,T> & origin()
	{
		return origin_;
	}

	/// Access the origin of the ray.
	//!
	//! /return Returns a const reference to the origin of the ray.
	sgl::point<D,T> const & origin() const
	{
		return origin_;
	}

	/// Access the direction of the ray.
	//!
	//! /return Returns a reference to the direction of the ray.
	sgl::direction<D,T> & direction()
	{
		return direction_;
	}

	/// Access the direction of the ray.
	//!
	//! /return Returns a const reference to the direction of the ray.
	sgl::direction<D,T> const & direction() const
	{
		return direction_;
	}

	/// Unary negation.
	//!
	//! A ray is negated by negating its direction while leaving its origin
	//! untouched.
	//!
	//! \return A negated ray object.
    sgl::ray<D,T> operator - () const
    {
        return sgl::ray<D,T>(origin_, -direction_);
    }

	/// Equality operator.
	//!
	//! Two rays are compared for equality by comparing their origin and 
	//! direction in a pairwise manner. The rays are equal only if all pairwise 
	//! comparisons are equal.
	//!
	//! Through the use of boost::equality as a base class operator != is 
	//! defined as well and implemented in terms of this operator ==.
	//!
	//! \return true if equal, false otherwise.
    bool operator == (
		/// Right hand side.
		sgl::ray<D,T> const& rhs) const
    {
		SGL_PRECONDITION(D == rhs.dimensions(),
			"dimension mismatch.");

		return 
			origin_ == rhs.origin_ &&
			direction_ == rhs.direction_;
    }

	/// Add a vector to a ray.
	//!
	//! Adding a vector to a ray moves the ray in direction of the vector.
	//! Addition is implemented by pairwise addition of the components of the 
	//! rays origin and the vector.
	//!
	//! Through the use of boost::additive as a base class operator + is 
	//! defined as well and implemented in terms of this operator +=.
	//!
	//! \return A reference to this object.
    sgl::ray<D,T> & operator += (
		/// Right hand side.
		sgl::vector<D,T> const& rhs)
    {
		SGL_PRECONDITION(D == rhs.dimensions(),
			"dimension mismatch.");

		origin_ += rhs;

        return *this;
    }

	/// Subtract a vector from a ray.
	//!
	//! Subtracting a vector from a ray moves the ray in the opposite 
	//! direction of the vector. Subtraction is implemented by pairwise 
	//! subtraction of the components of the rays origin and the vector.
	//!
	//! Through the use of boost::additive as a base class operator - is 
	//! defined as well and implemented in terms of this operator -=.
	//!
	//! \return A reference to this object.
    sgl::ray<D,T> & operator -= (
		/// Right hand side.
		sgl::vector<D,T> const& rhs)
    {
		SGL_PRECONDITION(D == rhs.dimensions(),
			"dimension mismatch.");

		origin_ -= rhs;

		return *this;
    }

private:
	// The starting point of the ray.
	sgl::point<D,T> origin_;

	/// The direction of the ray.
	sgl::direction<D,T> direction_;
};


/// A two-dimensional ray with the default component type.
typedef sgl::ray<2> ray2;

/// A two-dimensional ray with float component type.
typedef sgl::ray<2, float> ray2f;

/// A two-dimensional ray with double component type.
typedef sgl::ray<2, double> ray2d;


/// A three-dimensional ray with the default component type.
typedef sgl::ray<3> ray3;

/// A three-dimensional ray with float component type.
typedef sgl::ray<3, float> ray3f;

/// A three-dimensional ray with double component type.
typedef sgl::ray<3, double> ray3d;


} // namespace sgl

#endif // __sgl_ray_h__
